﻿
using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using DBUtility;

namespace XQ.SP.DalServices
{
    //部门角色配置
    public partial class SysDepaSeleRolesDal : BaseDal
    {

        public bool ExistsWhere(string whereSql)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SysDepaSeleRoles");
            if (whereSql != "")
            {
                strSql.Append(" where " + whereSql);
            }

            return DbHelperSQL.Exists(strSql.ToString());
        }
        public bool Exists(long id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [SysDepaSeleRoles]");
            strSql.Append(" where  [Id]=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(XQ.SP.Model.SysDepaSeleRolesItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [SysDepaSeleRoles](");
            strSql.Append("[RolesId],[RolesName],[DepaId],[DepaName],[ParentId],[Name],[Description],[Status],[Creator],[Created],[Modifier],[Modified]");
            strSql.Append(") values (");
                strSql.Append("@RolesId,@RolesName,@DepaId,@DepaName,@ParentId,@Name,@Description,@Status,@Creator,@Created,@Modifier,@Modified");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            
                        SqlParameter[] parameters = {
			            new SqlParameter("@RolesId", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@RolesName", SqlDbType.NVarChar,160) ,            
			            new SqlParameter("@DepaId", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@DepaName", SqlDbType.NVarChar,160) ,            
			            new SqlParameter("@ParentId", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@Name", SqlDbType.NVarChar,80) ,            
			            new SqlParameter("@Description", SqlDbType.NVarChar,1000) ,            
			            new SqlParameter("@Status", SqlDbType.Int,4) ,            
			            new SqlParameter("@Creator", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@Created", SqlDbType.DateTime,8) ,            
			            new SqlParameter("@Modifier", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@Modified", SqlDbType.DateTime,8) ,            
            };
            
            parameters[0].Value = model.RolesId;
            parameters[1].Value = model.RolesName;
            parameters[2].Value = model.DepaId;
            parameters[3].Value = model.DepaName;
            parameters[4].Value = model.ParentId;
            parameters[5].Value = model.Name;
            parameters[6].Value = model.Description;
            parameters[7].Value = model.Status;
            parameters[8].Value = model.Creator;
            parameters[9].Value = model.Created;
            parameters[10].Value = model.Modifier;
            parameters[11].Value = model.Modified;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }

        }

        /// <summary>
        ///   批量添加数据
        /// </summary>
        public int AddList(List<XQ.SP.Model.SysDepaSeleRolesItem> models)
        {
            List<CommandInfo> commands = new List<CommandInfo>();
            foreach (XQ.SP.Model.SysDepaSeleRolesItem model in models)
            {
                CommandInfo command = new CommandInfo();
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into [SysDepaSeleRoles](");
                strSql.Append("[RolesId],[RolesName],[DepaId],[DepaName],[ParentId],[Name],[Description],[Status],[Creator],[Created],[Modifier],[Modified]");
                strSql.Append(") values (");
                strSql.Append("@RolesId,@RolesName,@DepaId,@DepaName,@ParentId,@Name,@Description,@Status,@Creator,@Created,@Modifier,@Modified");
                strSql.Append(") ");
                strSql.Append(";select @@IDENTITY");
            
                        SqlParameter[] parameters = {
			            new SqlParameter("@RolesId", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@RolesName", SqlDbType.NVarChar,160) ,            
			            new SqlParameter("@DepaId", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@DepaName", SqlDbType.NVarChar,160) ,            
			            new SqlParameter("@ParentId", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@Name", SqlDbType.NVarChar,80) ,            
			            new SqlParameter("@Description", SqlDbType.NVarChar,1000) ,            
			            new SqlParameter("@Status", SqlDbType.Int,4) ,            
			            new SqlParameter("@Creator", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@Created", SqlDbType.DateTime,8) ,            
			            new SqlParameter("@Modifier", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@Modified", SqlDbType.DateTime,8) ,            
            };
            
            parameters[0].Value = model.RolesId;
            parameters[1].Value = model.RolesName;
            parameters[2].Value = model.DepaId;
            parameters[3].Value = model.DepaName;
            parameters[4].Value = model.ParentId;
            parameters[5].Value = model.Name;
            parameters[6].Value = model.Description;
            parameters[7].Value = model.Status;
            parameters[8].Value = model.Creator;
            parameters[9].Value = model.Created;
            parameters[10].Value = model.Modifier;
            parameters[11].Value = model.Modified;
                command.CommandText = strSql.ToString();
                command.Parameters = parameters;
                commands.Add(command);
            }
            object obj = DbHelperSQL.ExecuteSqlTran(commands);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 批量更新数据
        /// </summary>
        public int UpdateList(List<XQ.SP.Model.SysDepaSeleRolesItem> models)
        {
            List<CommandInfo> commands = new List<CommandInfo>();
            CommandInfo command = new CommandInfo();

            foreach (XQ.SP.Model.SysDepaSeleRolesItem model in models)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update [SysDepaSeleRoles] set ");
            
                strSql.Append(" [RolesId] = @RolesId,");
                strSql.Append(" [RolesName] = @RolesName,");
                strSql.Append(" [DepaId] = @DepaId,");
                strSql.Append(" [DepaName] = @DepaName,");
                strSql.Append(" [ParentId] = @ParentId,");
                strSql.Append(" [Name] = @Name,");
                strSql.Append(" [Description] = @Description,");
                strSql.Append(" [Status] = @Status,");
                strSql.Append(" [Creator] = @Creator,");
                strSql.Append(" [Created] = @Created,");
                strSql.Append(" [Modifier] = @Modifier,");
                strSql.Append(" [Modified] = @Modified");
                strSql.Append(" where [Id]=@Id ");
            
                        SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@RolesId", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@RolesName", SqlDbType.NVarChar,160) ,            
			            new SqlParameter("@DepaId", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@DepaName", SqlDbType.NVarChar,160) ,            
			            new SqlParameter("@ParentId", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@Name", SqlDbType.NVarChar,80) ,            
			            new SqlParameter("@Description", SqlDbType.NVarChar,1000) ,            
			            new SqlParameter("@Status", SqlDbType.Int,4) ,            
			            new SqlParameter("@Creator", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@Created", SqlDbType.DateTime,8) ,            
			            new SqlParameter("@Modifier", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@Modified", SqlDbType.DateTime,8) ,            
            };
            
            parameters[0].Value = model.Id;
            parameters[1].Value = model.RolesId;
            parameters[2].Value = model.RolesName;
            parameters[3].Value = model.DepaId;
            parameters[4].Value = model.DepaName;
            parameters[5].Value = model.ParentId;
            parameters[6].Value = model.Name;
            parameters[7].Value = model.Description;
            parameters[8].Value = model.Status;
            parameters[9].Value = model.Creator;
            parameters[10].Value = model.Created;
            parameters[11].Value = model.Modifier;
            parameters[12].Value = model.Modified;
                command.CommandText = strSql.ToString();
                command.Parameters = parameters;
                commands.Add(command);
            }

            int rows = DbHelperSQL.ExecuteSqlTran(commands);
            return rows;

        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(XQ.SP.Model.SysDepaSeleRolesItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [SysDepaSeleRoles] set ");
            
                strSql.Append(" [RolesId] = @RolesId,");
                strSql.Append(" [RolesName] = @RolesName,");
                strSql.Append(" [DepaId] = @DepaId,");
                strSql.Append(" [DepaName] = @DepaName,");
                strSql.Append(" [ParentId] = @ParentId,");
                strSql.Append(" [Name] = @Name,");
                strSql.Append(" [Description] = @Description,");
                strSql.Append(" [Status] = @Status,");
                strSql.Append(" [Creator] = @Creator,");
                strSql.Append(" [Created] = @Created,");
                strSql.Append(" [Modifier] = @Modifier,");
                strSql.Append(" [Modified] = @Modified");
                strSql.Append(" where [Id]=@Id ");
            
                        SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@RolesId", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@RolesName", SqlDbType.NVarChar,160) ,            
			            new SqlParameter("@DepaId", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@DepaName", SqlDbType.NVarChar,160) ,            
			            new SqlParameter("@ParentId", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@Name", SqlDbType.NVarChar,80) ,            
			            new SqlParameter("@Description", SqlDbType.NVarChar,1000) ,            
			            new SqlParameter("@Status", SqlDbType.Int,4) ,            
			            new SqlParameter("@Creator", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@Created", SqlDbType.DateTime,8) ,            
			            new SqlParameter("@Modifier", SqlDbType.BigInt,8) ,            
			            new SqlParameter("@Modified", SqlDbType.DateTime,8) ,            
            };
            
            parameters[0].Value = model.Id;
            parameters[1].Value = model.RolesId;
            parameters[2].Value = model.RolesName;
            parameters[3].Value = model.DepaId;
            parameters[4].Value = model.DepaName;
            parameters[5].Value = model.ParentId;
            parameters[6].Value = model.Name;
            parameters[7].Value = model.Description;
            parameters[8].Value = model.Status;
            parameters[9].Value = model.Creator;
            parameters[10].Value = model.Created;
            parameters[11].Value = model.Modifier;
            parameters[12].Value = model.Modified;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SysDepaSeleRoles ");
            strSql.Append(" where Id=@Id  ");
            SqlParameter[] parameters = {new SqlParameter("@Id", SqlDbType.BigInt)};
            parameters[0].Value = Id;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SysDepaSeleRoles ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public XQ.SP.Model.SysDepaSeleRolesItem GetModel(long Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *  ");
            strSql.Append("  from SysDepaSeleRoles ");
            strSql.Append(" where Id=@Id  ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.BigInt)
			};
            parameters[0].Value = Id;


            XQ.SP.Model.SysDepaSeleRolesItem model = new XQ.SP.Model.SysDepaSeleRolesItem();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = Int64.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RolesId"].ToString() != "")
                {
                    model.RolesId = Int64.Parse(ds.Tables[0].Rows[0]["RolesId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RolesName"].ToString() != "")
                {
                    model.RolesName = ds.Tables[0].Rows[0]["RolesName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DepaId"].ToString() != "")
                {
                    model.DepaId = Int64.Parse(ds.Tables[0].Rows[0]["DepaId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DepaName"].ToString() != "")
                {
                    model.DepaName = ds.Tables[0].Rows[0]["DepaName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ParentId"].ToString() != "")
                {
                    model.ParentId = Int64.Parse(ds.Tables[0].Rows[0]["ParentId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Description"].ToString() != "")
                {
                    model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Status"].ToString() != "")
                {
                    model.Status = Int32.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Creator"].ToString() != "")
                {
                    model.Creator = Int64.Parse(ds.Tables[0].Rows[0]["Creator"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Created"].ToString() != "")
                {
                    model.Created = System.DateTime.Parse(ds.Tables[0].Rows[0]["Created"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Modifier"].ToString() != "")
                {
                    model.Modifier = Int64.Parse(ds.Tables[0].Rows[0]["Modifier"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Modified"].ToString() != "")
                {
                    model.Modified = System.DateTime.Parse(ds.Tables[0].Rows[0]["Modified"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public XQ.SP.Model.SysDepaSeleRolesItem GetModelWhereStr(string whereStr)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *  ");
            strSql.Append("  from SysDepaSeleRoles ");
            if (whereStr != "")
            {
                strSql.Append(" where " + whereStr);
            }

            XQ.SP.Model.SysDepaSeleRolesItem model = new XQ.SP.Model.SysDepaSeleRolesItem();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());

            if (ds.Tables[0].Rows.Count > 0)
            {
                
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = Int64.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RolesId"].ToString() != "")
                {
                    model.RolesId = Int64.Parse(ds.Tables[0].Rows[0]["RolesId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RolesName"].ToString() != "")
                {
                    model.RolesName = ds.Tables[0].Rows[0]["RolesName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DepaId"].ToString() != "")
                {
                    model.DepaId = Int64.Parse(ds.Tables[0].Rows[0]["DepaId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DepaName"].ToString() != "")
                {
                    model.DepaName = ds.Tables[0].Rows[0]["DepaName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ParentId"].ToString() != "")
                {
                    model.ParentId = Int64.Parse(ds.Tables[0].Rows[0]["ParentId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Description"].ToString() != "")
                {
                    model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Status"].ToString() != "")
                {
                    model.Status = Int32.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Creator"].ToString() != "")
                {
                    model.Creator = Int64.Parse(ds.Tables[0].Rows[0]["Creator"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Created"].ToString() != "")
                {
                    model.Created = System.DateTime.Parse(ds.Tables[0].Rows[0]["Created"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Modifier"].ToString() != "")
                {
                    model.Modifier = Int64.Parse(ds.Tables[0].Rows[0]["Modifier"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Modified"].ToString() != "")
                {
                    model.Modified = System.DateTime.Parse(ds.Tables[0].Rows[0]["Modified"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM SysDepaSeleRoles ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM SysDepaSeleRoles ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

    }
}

